package com.client.elements.canvas;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import com.client.elements.canvas.tools.Kreis;
import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class Watch {

	static Canvas canvas;
	static Context2d context;

	final static Button btn = new Button("Start");

	static Kreis gross = new Kreis(150, 200, 200, 0, 0);
	static Kreis mitte = new Kreis(15, 200, 200, 0, 0);

	public static Widget watch() {

		FlowPanel main = new FlowPanel();

		canvas = Canvas.createIfSupported();

		canvas.setCoordinateSpaceWidth(400);
		canvas.setCoordinateSpaceHeight(400);
		canvas.addStyleName("border");

		context = canvas.getContext2d();

		timer(true);

		setSecondPositions();
		setMinutePositions();
		setStundenPositions();

		btn.addClickHandler(new ClickHandler() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(ClickEvent event) {
				if (btn.getText().equals("Start")) {
					anzahl_s = new Date().getSeconds();
					anzahl_m = new Date().getMinutes();
					anzahl_h = new Date().getHours();


					timer(false);
					btn.setText("Stop");
					gross.update(context);
					mitte.update_bunt(context, "black");
					zeichneStriche();
				}else if (btn.getText().equals("Stop")) {
					timer(true);
					btn.setText("Start");
				}
			}
		});

		main.add(canvas);
		main.add(btn);

		return main;
	}

	final static Timer timer = new Timer() {
		@Override
		public void run() {
			drawWatch();
		}
	};

	private static void timer(boolean stop) {
		if (stop == false) {
			timer.scheduleRepeating(1000);		
		}else {
			timer.cancel();
		}
	}

	static ArrayList<ArrayList<Double>> sekundenPositionen = new ArrayList<ArrayList<Double>>();
	static void setSecondPositions(){
		int winkel_sec = 180;
		for (int i = 0; i < 60; i++) {
			double pos_x = Math.sin(Math.toRadians(winkel_sec)) * 150 + 200;
			double pos_y = Math.cos(Math.toRadians(winkel_sec)) * 150 + 200;

			sekundenPositionen.add(i, new ArrayList<Double>(Arrays.asList(new Double[]{pos_x, pos_y})));

			winkel_sec -= 6;
		}
	}

	static ArrayList<ArrayList<Double>> minutenPositionen = new ArrayList<ArrayList<Double>>();
	static void setMinutePositions(){
		int winkel_min = 180;
		for (int i = 0; i < 60; i++) {
			double pos_x = Math.sin(Math.toRadians(winkel_min)) * 100 + 200;
			double pos_y = Math.cos(Math.toRadians(winkel_min)) * 100 + 200;

			minutenPositionen.add(i, new ArrayList<Double>(Arrays.asList(new Double[]{pos_x, pos_y})));

			winkel_min -= 6;
		}
	}

	static ArrayList<ArrayList<Double>> stundenPositionen = new ArrayList<ArrayList<Double>>();
	static void setStundenPositions(){
		int winkel_h = 180;
		for (int i = 0; i < 60; i++) {
			double pos_x = Math.sin(Math.toRadians(winkel_h)) * 50 + 200;
			double pos_y = Math.cos(Math.toRadians(winkel_h)) * 50 + 200;

			stundenPositionen.add(i, new ArrayList<Double>(Arrays.asList(new Double[]{pos_x, pos_y})));

			winkel_h -= 30;
		}
	}

	static void zeichneStriche(){
		for (int i = 0; i < sekundenPositionen.size(); i++) {
			double pos_x = sekundenPositionen.get(i).get(0);
			double pos_y = sekundenPositionen.get(i).get(1);
			context.beginPath();
			if (i % 5 == 0) {
				context.setFillStyle("red");
				context.arc(pos_x, pos_y, 5, 0, Math.PI * 2, true);
			}else {
				context.setFillStyle("black");
				context.arc(pos_x, pos_y, 3, 0, Math.PI * 2, true);
			}
			context.closePath();
			context.fill();
		}
	}

	static int anzahl_s = 0;
	static int anzahl_m = 0;
	static int anzahl_h = 0;

	public static void drawWatch(){
		context.clearRect(0, 0, 400, 400);

		gross.update(context);
		mitte.update_bunt(context, "black");
		zeichneStriche();

		context.beginPath();
		{//Sekunden
			double pos_x = sekundenPositionen.get(anzahl_s).get(0);
			double pos_y = sekundenPositionen.get(anzahl_s).get(1);
			context.moveTo(200, 200);
			context.lineTo(pos_x, pos_y);
		}

		{//Minuten
			double pos_x = minutenPositionen.get(anzahl_m).get(0);
			double pos_y = minutenPositionen.get(anzahl_m).get(1);
			context.moveTo(200, 200);
			context.lineTo(pos_x, pos_y);
		}

		{//Stunden
			double pos_x = stundenPositionen.get(anzahl_h).get(0);
			double pos_y = stundenPositionen.get(anzahl_h).get(1);
			context.moveTo(200, 200);
			context.lineTo(pos_x, pos_y);
		}
		context.closePath();
		context.stroke();

		anzahl_s += 1;
		if (anzahl_s == 60) {
			anzahl_s = 0;
			anzahl_m += 1;
		}

		if (anzahl_m == 60) {
			anzahl_m = 0;
			anzahl_h+= 1;
		}

		if (anzahl_h == 12) {
			anzahl_h = 0;
		}
	}
}
